/*
<license>
Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

This file is part of quicktag <http://code.google.com/p/quicktag/>

quicktag is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

quicktag is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this code.
If not, see <http://www.gnu.org/licenses/>.
</license>
*/

#include <generik/tracing.h>
GENERIK_TRACER_GLOBAL;

//#include <quicktag/quicktag.h>
#include <quicktag/reader.h>

// this is buggy
//#include "wavpack_tag.h"

#include <iostream>
#include <vector>
#include <list>
#include <map>

#include <generik/osutils.h>
#include <generik/termutils.h>

#include <sstream>


// ----------------------------------------------------

struct entry_collector
{
	int total;
	std::list<std::string> entries;
	
	entry_collector()
	: total(0)
	{}
	
	void operator()(const std::string& path)
	{
		// check for an mp3 extension here?
		entries.push_back(path);
		total++;
	}
};

// ----------------------------------------------------

int main(int argc, char** argv)
{
	Charset_Insert_Locales_Init();

	// use the argument list to traverse specified directories
	std::string path;
	if (argc > 1)
	{
		path = argv[1];
	}
	else path = generik::getcwd();

	entry_collector dc;
	entry_collector fc;
	
	std::cout<<"Collecting filenames in ["<<path<<"]..."<<std::endl;
	// check wether the specified path is actually a path
	//if (filetype(path) == DIR)
	//{
	
		// collect all files in the path
		generik::process_path(path, fc, dc);
	
	//}
	//else
	//{
	//	// otherwise simply add it as a lone entry into the file collector
	//	fc(path);
	//}
	
	/*
	for (std::list<std::string>::iterator i = dc.entries.begin(); i != dc.entries.end(); ++i)
	{
		std::cout<<*i<<std::endl;
	}
	*/
	
	// we now have a list of files which we can process
	
	// the following code process each file in a provided list,
	// given an (optional) object and callback
	
	std::cout<<"Processing "<<fc.total<<" files in "<<dc.total<<" directories:"<<std::endl;
	clock_t start = clock();
	//
	generik::progress_bar_counter progress_bar_counter;
	quicktag::reader p(fc.entries, &generik::progress_bar, (void*)&progress_bar_counter);
	//
	clock_t stop = clock();
	std::cout<<"Executed in "<<((float)stop-(float)start)/CLOCKS_PER_SEC<<" seconds"<<std::endl;
	
	// ouput the results
	quicktag::artist_map all = p.results();
	std::cout<<"RESULTS:"<<std::endl;
	for (std::map<std::string, quicktag::artist_type>::iterator i = all.artists.begin(); i != all.artists.end(); ++i)
	{
		std::cout<<"'"<<i->first<<"'"<<std::endl;
		for (std::map<std::string, quicktag::album_type>::iterator a = i->second.albums.begin(); a != i->second.albums.end(); ++a)
		{
			std::cout<<"     '"<<a->first<<"'"<<std::endl;
			for (std::list<quicktag::track_type>::iterator t = a->second.tracks.begin(); t != a->second.tracks.end(); ++t)
			{
				std::cout<<"          '"<<t->title<<"'"<<std::endl;
			}
		}
	}
	
	std::cout<<"UNHANDLED FILES:"<<std::endl;
	for (std::list<std::string>::iterator i = p.unhandled_files.begin(); i != p.unhandled_files.end(); ++i)
	{
		std::cout<<*i<<std::endl;
	}
	
	std::cout<<"FAILED FILES:"<<std::endl;
	for (std::list<std::string>::iterator i = p.failed_files.begin(); i != p.failed_files.end(); ++i)
	{
		std::cout<<*i<<std::endl;
	}
	return 0;
}








